package com.pansoft.kline.adapter;

import android.view.View;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.pansoft.kline.activity.TransactionTrendActivity;
import com.pansoft.kline.model.KLineRiseFallItemModel;
import com.pansoft.kline.model.KLineRiseFallModel;
import com.pansoft.openplanet.R;

import java.util.List;

/**
 * @author : zzj
 * @e-mail : zhangzhijun@pansoft.com
 * @date : 2018/11/19 15:39
 * @desc : 涨跌幅的展开式数据结构的adapter
 * @version: 1.0
 */
public class KLineRiseFallAdapter extends BaseMultiItemQuickAdapter<MultiItemEntity, BaseViewHolder> {

    /**
     * 一级目录
     */
    public static final int TYPE_RISE_FALL_LEVEL_ONE = 1;
    /**
     * 二级目录
     */
    public static final int TYPE_RISE_FALL_LEVEL_TWO = 2;
    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param data A new list is created out of this one to avoid mutable list
     */
    public KLineRiseFallAdapter(List<MultiItemEntity> data) {
        super(data);
        addItemType(TYPE_RISE_FALL_LEVEL_ONE, R.layout.kline_item_rise_fall_line_title);
        addItemType(TYPE_RISE_FALL_LEVEL_TWO, R.layout.kline_item_rise_fall_line_child);
    }

    @Override
    protected void convert(final BaseViewHolder helper, MultiItemEntity item) {
        switch (helper.getItemViewType()){
            case TYPE_RISE_FALL_LEVEL_ONE:
                final KLineRiseFallModel kLineRiseFallModel = (KLineRiseFallModel) item;
                helper.setText(R.id.tv_title,kLineRiseFallModel.getTitle())
                        .setImageResource(R.id.iv_arrow,
                                kLineRiseFallModel.isExpanded()? R.drawable.arrow_down : R.drawable.arrow_left);
                helper.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int pos = helper.getAdapterPosition();
                        if (kLineRiseFallModel.isExpanded()) {
                            collapse(pos);
                        } else {
                            expand(pos);
                        }
                    }
                });
                helper.getView(R.id.tv_check_all).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int pos = helper.getAdapterPosition();
                        if (kLineRiseFallModel.isExpanded()) {
//                            collapse(pos);
                        } else {
                            expand(pos);
                        }
                    }
                });
                break;
            case TYPE_RISE_FALL_LEVEL_TWO:
                final KLineRiseFallItemModel kLineRiseFallItemModel = (KLineRiseFallItemModel) item;
                helper.setText(R.id.tv_name,kLineRiseFallItemModel.getName());
                helper.setText(R.id.tv_full_name,kLineRiseFallItemModel.getFullName());
                helper.setText(R.id.tv_price,kLineRiseFallItemModel.getPrice());
                helper.setText(R.id.tv_sub_price,kLineRiseFallItemModel.getSubPrice());
                helper.setText(R.id.btn_change_rate,kLineRiseFallItemModel.getChangeNum());
                if(kLineRiseFallItemModel.isIncrease()){
                    helper.setBackgroundRes(R.id.btn_change_rate,R.drawable.kline_shape_btn_item__change_green_bg);
                }else {
                    helper.setBackgroundRes(R.id.btn_change_rate,R.drawable.kline_shape_btn_item__change_red_bg);
                }
                helper.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        TransactionTrendActivity.start(mContext,kLineRiseFallItemModel.getMarketId());
                    }
                });
                break;

                default:
        }
    }
}
